$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'sys/coupon/couponList',
        datatype: "json",
		async:false,
		colModel: [
			{ label: '优惠券编号', name: 'id', index: 'id',align: "center", width: 50},
			{ label: '公众号标识', name: 'gzhName', index: 'gzh_code',align: "center", width: 50},
			{ label: '优惠价格1', name: 'priceOne', index: 'price_one',align: "center", width: 80 },
			{ label: '优惠价格2', name: 'priceTwo', index: 'price_two',align: "center", width: 80 },
			{ label: '优惠价格3', name: 'priceThree', index: 'price_three',align: "center", width: 80 },
			{ label: '优惠价格4', name: 'priceFour', index: 'price_four',align: "center", width: 80 },
			{ label: '是否有效', name: 'enabled', index: 'enabled',align: "center", width: 80 ,formatter:function(value,options,rowData){
					if( value===1 ){
						return '有效';
					}else{
						return '无效';
					}
				}},
			{ label: '有效开始时间', name: 'startTime', index: 'start_time',align: "center", width: 80 },
			{ label: '有效结束时间', name: 'endTime', index: 'end_time',align: "center", width: 80 },
			{ label: '创建时间', name: 'createTime', index: 'create_time',align: "center", width: 80 },
			/*{ label: '修改时间', name: 'modifyTime', index: 'modify_time',align: "center", width: 80 },*/
			{ label: '创建人编号', name: 'creatorUserId', index: 'creator_user_id',align: "center", width: 80 },
			/*{ label: '用户编号', name: 'userId', index: 'user_id',align: "center", width: 80 },
			{ label: '优惠券类型', name: 'couponType', index: 'coupon_type',align: "center", width: 80 ,formatter:function(value,options,rowData){
					if( value=== 0 ){
						return '公众号';
					}else{
						return '个人';
					}
				}},*/
			{ label: '操作', name: '', index: '',align: "center", width: 130,
				formatter:function(value,options,rowData){
					var id=rowData["id"];
					return "<button class='btn btn-primary'  onclick=vm.addCoupon("+id+",1)>"+'修改'+"</button>"+"&nbsp;"+
						"<button class='btn btn-primary'  onclick=vm.rem("+id+")>"+'删除'+"</button>";
				}
			}

        ],
		viewrecords: true,
		height: "100%",
		rowNum: 8,
		rowList : [8,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: false,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
		/*postData:{'user_id':vm.q.user_id},*/
        prmNames : {
            page:"page", 
            rows:"limit",
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
	$.ajax({
		type: "POST",
		async:false,
		url: baseURL + 'sys/buserinfo/getUserGzhList',
		contentType: "application/json",
		success: function(r){
			if(r.code==0){
				//vm.bbrandinfo=r.brandInfo;
				$("#selectUserGzhList").append(
					"<option value=''>全部平台</option>");
				for(var i=0;i<r.userGzhList.length;i++){
					$("#selectUserGzhList").append(
						"<option value=" + r.userGzhList[i].gzhCode +">"+ r.userGzhList[i].gzhName+"</option>");
				}
			}
		}
	});
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			user_id:null,
			gzhCode:null,
			id:null
		},
		showList: true,
		addUser: true,
		title: null,
		bUserInfo: {},
		couponInfo: {},
		backStatus:0,
		id:0
	},
	methods: {
		query: function () {
			vm.showList = true;
			$("#jqGrid").jqGrid('setGridParam', {
				postData: {
					'gzhCode': vm.q.gzhCode
				},
				page: 1
			}).trigger("reloadGrid");
		},
		rem:function(e){
			$.ajax({
				type: "POST",
				url: baseURL + "sys/coupon/delCoupon/",
				contentType: "application/json",
				data: JSON.stringify(e),
				success: function(r){
					if(r.code==0){
						alert('删除成功', function(index){
							$("#jqGrid").trigger("reloadGrid");
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		addCoupon: function(id,event){
			window.open("../sys/bcoupon_edit.html?id="+id);
		},
		updateInfo: function (event) {
			var url = vm.couponInfo.id == null ? "sys/coupon/addCoupon" : "sys/coupon/updateCoupon";
			vm.couponInfo.gzhCode=$('#gzhCodes').val();
			vm.couponInfo.startTime=$('#startTime').val().replace(/T/ig, ' ');
			vm.couponInfo.endTime=$('#endTime').val().replace(/T/ig, ' ');
			$.ajax({
				type: "POST",
				url: baseURL + url,
				contentType: "application/json;charset=UTF-8",
				data:JSON.stringify(vm.couponInfo),
				dataType: "JSON",
				success: function(r){
					if(r.code === 0){
						alert('操作成功', function(index){
							window.close();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
	},
});
